define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-add").data("area",["1000px","600px"]);
            $(".btn-edit").data("area",["1000px","600px"]);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'detection/index' + location.search,
                    add_url: 'detection/add',
                    edit_url: 'detection/edit',
                    del_url: 'detection/del',
                    multi_url: 'detection/multi',
                    import_url: 'detection/import',
                    table: 'detection',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                showToggle: false,
                search:false,
                showColumns: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'unit_name', title: __('Unit_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'address', title: __('Address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'producer', title: __('Producer'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'producer_time', title: __('Producer_time'), operate: false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'sample_name', title: __('样品名称'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'sample_num', title: __('样品数量'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'sample_type', title: __('样品类型'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'smple_time', title: __('Smple_time'), operate: false,addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'detection_time', title: __('Detection_time'), addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'create_time', title: __('Create_time'), operate: false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        // {field: 'unit_id', title: __('Unit_id')},
                        // {field: 'unit.id', title: __('Unit.id')},
                        // {field: 'unit.name', title: __('Unit.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'unit.address', title: __('Unit.address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'unit.producer', title: __('Unit.producer'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'unit.phone', title: __('Unit.phone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter:function(value,row,index){  //新起一个
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if (row.status !=3) {
                                    $(table).data("operate-edit",null);//隐藏操作中的移动按钮，
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                                // var that = $.extend({},this);//将this赋值给that,
                                // var table = $(that.table).clone(true);//通过that去引用table中的信息
                                // $(table).data("operate-edit",null);//隐藏操作中的编辑按钮，
                                // $(table).data("operate-del",null);//隐藏操作中的删除按钮，
                                // $(table).data("operate-dragsort",null);//隐藏操作中的移动按钮，
                                // that.table = table;
                                // return Table.api.formatter.operate.call(that,value,row,index);//展示信息
                            },}
                    ]
                ]
            });
            table.on('post-body.bs.table',function(){
                $(".btn-editone").data("area",["1400px","600px"]);
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Template.helper("Fast", Fast);

            $(document).on("fa.event.appendfieldlist", "#second-table .btn-append", function (e, obj) {
                //绑定动态下拉组件
                Form.events.selectpage(obj);
                //绑定日期组件
                Form.events.datetimepicker(obj);
                //绑定上传组件
                Form.events.faupload(obj);
                //上传成功回调事件，变更按钮的背景
                $(".upload-image", obj).data("upload-success", function (data) {
                    $(this).css("background-image", "url('" + Fast.api.cdnurl(data.url) + "')");
                })
                $(".upload-images", obj).data("upload-success", function (data) {
                    $(this).css("background-image", "url('" + Fast.api.cdnurl(data.url) + "')");
                })
            });
            $(document).on('click','.btn-primary',function(){
                Form.api.submit($("form[role=form]"),function(){
                    window.parent.location.reload();
                });
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Template.helper("Fast", Fast);
            $(document).on("fa.event.appendfieldlist", "#second-table .btn-append", function (e, obj) {
                //绑定动态下拉组件
                Form.events.selectpage(obj);
                //绑定日期组件
                Form.events.datetimepicker(obj);
                //绑定上传组件
                Form.events.faupload(obj);


                //上传成功回调事件，变更按钮的背景
                $(".upload-image", obj).data("upload-success", function (data) {
                    $(this).css("background-image", "url('" + Fast.api.cdnurl(data.url) + "')");
                })
                $(".upload-images", obj).data("upload-success", function (data) {
                    $(this).css("background-image", "url('" + Fast.api.cdnurl(data.url) + "')");
                })
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
function chang(){
    var loan_id = $("#c-unit_id" ) .val();
    $.ajax({
        url: '/adminweb.php/unit/getone?id='+loan_id,
        data: '',
        dataType: 'json',
        contentType: false,
        processData: false,
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            console.log(data)
            console.log(Array.isArray(data.data))
            if(Array.isArray(data.data)){
                document.getElementById('c-address').value = ''
                document.getElementById('c-name').value = ''
                document.getElementById('c-phone').value = 'unit.phone'
            }else{
                var unit = data.data;
                document.getElementById('c-address').value = unit.address
                document.getElementById('c-name').value = unit.producer
                document.getElementById('c-phone').value = unit.phone
            }

        },

    });
    console.log(loan_id)
}


// function handleSubmit(){
//     window.location.reload();
// }